package br.unip.tcc.domotic.controller;

import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class MenuController {
	
	
	public String homeInsert(){
		MenuController.clearBeanFromController("homeController");
		return "/domotic/residencia.jsf";
	}
	public String homeSearch(){
		MenuController.clearBeanFromController("homeController");
		MenuController.clearSearchedBeansFromController("homeController");
		return "/domotic/residencia_consulta.jsf";
	}
	public String homeList(){
		MenuController.clearBeanFromController("homeController");
		return "/domotic/residencia_consulta.jsf";
	}
	
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void clearBeanFromController(String controllerName){
		Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		if (sessionMap.containsKey(controllerName)){
			AbstractController abstractController = (AbstractController)sessionMap.get(controllerName);
			abstractController.setBean(abstractController.getBeanInstance());
		}
	}
	@SuppressWarnings( "rawtypes" )
	public static void clearSearchedBeansFromController(String controllerName){
		Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		if (sessionMap.containsKey(controllerName)){
			AbstractController abstractController = (AbstractController)sessionMap.get(controllerName);
			if (abstractController.getSearchBeans() != null){ abstractController.getSearchBeans().clear(); }
		}
	}
	
}
